package zuo.algo.c10;

import java.util.LinkedList;

/**
 * 01:32
 * 给你一个栈，请你逆序这个栈，不能申请额外的数据结构，只能使用递归函数。
 * 如何实现?
 */
public class ReverseStackUsingRecursive {

    public static void exc1(LinkedList<Integer> stack) {
        reverse(stack);
    }

    public static void reverse(LinkedList<Integer> stack) {
        if (stack.isEmpty()) {
            return;
        }
        Integer num = f(stack);
        reverse(stack);
        stack.push(num);
    }

    public static Integer f(LinkedList<Integer> stack) {
        Integer num = stack.poll();
        if (stack.isEmpty()) {
            return num;
        }
        Integer res = f(stack);
        stack.push(num);
        return res;
    }

    public static void main(String[] args) {
        LinkedList<Integer> stack = new LinkedList<>();
        stack.push(3);
        stack.push(2);
        stack.push(1);
        exc1(stack);
        System.out.println();
    }
}
